using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Sedna.Core.Controls.Support
{
	public class CommonContainerLayout : IContainerLayout
	{

//		protected Table table;
		protected int rowNumber;
		protected int colNumber;
		protected string cssClass = "innertable";
		protected Unit width = Unit.Percentage(100);
		protected Unit height;
		protected BorderStyle border = BorderStyle.NotSet;
		protected int cellPadding = 0;
		protected int cellSpacing = -1;

		public int RowNumber
		{
			get{return rowNumber;}
			set{this.rowNumber = value;}
		}

		public int ColNumber
		{
			get{return colNumber;}
			set{this.colNumber = value;}
		}

		public string CssClass
		{
			get{return this.cssClass;}
			set{this.cssClass = value;}
		}

		public Unit Width
		{
			get{return width;}
			set{width = value;}
		}

		public Unit Height
		{
			get { return height; }
			set { height = value; }
		}

		public BorderStyle Border
		{
			get{return border;}
			set{this.border = value;}
		}

		public int CellPadding
		{
			get{return cellPadding;}
			set{this.cellPadding = value;}
		}

		public int CellSpacing
		{
			get{return cellSpacing;}
			set{this.cellSpacing = value;}
		}

		public CommonContainerLayout()
		{
		}

		public CommonContainerLayout(int rowNumber, int colNumber)
		{
			this.rowNumber = rowNumber;
			this.colNumber = colNumber;
		}


		public virtual void PopulateLayout(WebControl control, IList innerControls)
		{

			Table table = CreateTable(control);
			CreateRows(table);
			FillControls(table, innerControls);

			
		}

		protected virtual void FillControls(Table table, IList innerControls)
		{
			int [] colCount = new int[rowNumber];
			for(int i=0;i<rowNumber;i++)
			{
				colCount[i] = colNumber;
			}

			int curRow = 0;
			int curCol = 0;

			for(int i=0;i<innerControls.Count;i++)
			{
				if(innerControls[i] == null) continue;

				LayoutCell layoutCell = (LayoutCell)innerControls[i];

				TableCell td = new TableCell();
//				if (curCol%2 == 0) td.CssClass = "rightalign";
				if (curCol%2 == 0) td.HorizontalAlign = HorizontalAlign.Right;
				if (layoutCell.Width != 0) td.Width = layoutCell.Width;
				td.Height=layoutCell.Height;
				if (layoutCell.CssClass != null) td.CssClass = layoutCell.CssClass;
				table.Controls[curRow].Controls.Add(td);

				if(layoutCell.ColSpan>1) td.Attributes.Add("colSpan",layoutCell.ColSpan.ToString());
				if(layoutCell.RowSpan>1) td.Attributes.Add("rowSpan",layoutCell.RowSpan.ToString());
				if(layoutCell.NoWrap)	 td.Wrap = false;
				if(layoutCell.Align != HorizontalAlign.NotSet)	 td.HorizontalAlign = layoutCell.Align;
				if(layoutCell.Valign != VerticalAlign.NotSet)	 td.VerticalAlign = layoutCell.Valign;
				
				for(int j=0;j<layoutCell.Controls.Count;j++)
				{
					if (j>0)
					{
						LiteralControl nbspLiteralControl = new LiteralControl(null, null, "&nbsp");
						td.Controls.Add(nbspLiteralControl);
					}
					td.Controls.Add((Control)layoutCell.Controls[j]);
				}

				curCol += layoutCell.ColSpan;
				colCount[curRow] -= layoutCell.ColSpan;
				if(layoutCell.RowSpan >1)
				{
					for(int j=1;j<layoutCell.RowSpan;j++)
					{
						colCount[curRow+j] -= layoutCell.ColSpan;
					}
				}
				while( curRow<rowNumber && colCount[curRow]<=0 ) {curRow++;curCol = 0;}
			}
		}

		protected  virtual void CreateRows(Table table)
		{

			for(int i = 0; i < rowNumber; i++)
			{
				TableRow tr = new TableRow();
				table.Controls.Add(tr);

			}
			
		}

		protected virtual Table CreateTable(WebControl control)
		{
			Table table = new Table();
			if(cssClass != null)
			{
				table.CssClass = cssClass;
			}
			if(cellPadding>=0) table.CellPadding = cellPadding;
			if(cellSpacing>=0) table.CellSpacing = cellSpacing;
			table.Width = width;
			table.Height = height;
			control.Controls.Add(table);
			table.BorderStyle = border;
            return table;
		}

	}
}
